import express from "express";
import cors from "cors";
import morgan from "morgan";
import helmet from "helmet";
import { json, urlencoded } from "body-parser";
import authRoutes from "./routes/auth.route";
import userRoutes from "./routes/user.route";
import groupRoutes from "./routes/group.route";
import friendRoutes from "./routes/friend.route";
import messageRoutes from "./routes/message.route";
import conversationRoutes from "./routes/conversation.route";
import { errorHandler } from "./middlewares/errorHandler.middleware";
import { responseEnhancer } from "./middlewares/response.middleware";
import { requestLogger } from "./middlewares/requestLogger.middleware";
import { requestIdMiddleware } from './middlewares/requestId.middleware';
import { i18nMiddleware } from './middlewares/i18n.middleware';

// import './global';

const app = express();
app.use(responseEnhancer);
// 基础中间件
app.use(cors());
app.use(helmet());
app.use(json());
app.use(urlencoded({ extended: true }));
app.use(morgan("dev"));
app.use(requestIdMiddleware);
app.use(requestLogger);
app.use(i18nMiddleware);
app.use("/monster-im/auth", authRoutes);
app.use("/monster-im/user", userRoutes);
app.use("/monster-im/group", groupRoutes);
app.use("/monster-im/friend", friendRoutes);
app.use("/monster-im/message", messageRoutes);
app.use("/monster-im/conversation", conversationRoutes);
app.use(errorHandler); // 👈 放在最后处理异常
// TODO: 添加 JWT 中间件和路由
// import routes from './routes';
// app.use('/api', routes);

app.get("/", (req, res) => {
  res.send("Chat API is running");
});

export default app;
